#!/usr/local/bin/perl -w
use strict;
use utf8;
use lib '../lib';

use NG::Gen::App 'NG::Sys::Loop::Daemon';
use NG::Sys::Datetime;

my $app = new NG::Sys::App(logger => "daemon");

$app->run;

# called after fork
sub on_prepare_loop {
	my ($app) = @_;
	$app->{sth_check} = $app->dbh->prepare_cached("select `user_id` from `user_online` where `online`=1 and `checked` < ?");
}
# called to check possible do process
sub on_before_process {
	my($app) = @_;
	my $check = unixtime2datetime(time() - $app->users->{tune}{param}{session_long});
	$app->{sth_check}->execute($check);
	$app->{list} = $app->{sth_check}->fetchall_arrayref();
	return @{$app->{list}} > 0;
}
# main process
sub on_process {
	my($app) = @_;
	for my $row (@{$app->{list}}){
		my $user = $app->users->get_by_id($row->[0]);
		$user->do_user_logout;
	}
}

1;